package com.ruoyi.web.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.ServiceHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysCompany;
import com.ruoyi.system.domain.SysConsumer;
import com.ruoyi.system.domain.SysWorkOrder;
import com.ruoyi.system.service.ISysCompanyService;
import com.ruoyi.system.service.ISysConsumerService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class MpCompanyService {

    private final ServiceHelper helper;

    private final ISysCompanyService companyService;

    private final ISysConsumerService consumerService;

    /**
     * 获取列表
     *
     * @param keyword
     * @param page
     * @param limit
     * @return
     */
    public TableDataInfo getList(String keyword, Integer page, Integer limit) {
        TableDataInfo result = new TableDataInfo();
        LambdaQueryWrapper<SysCompany> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isNotEmpty(keyword)) {
            wrapper.like(SysCompany::getCompanyName, keyword);
        }
        wrapper.orderByDesc(SysCompany::getCreatedTime);
        Page<SysCompany> sysCompanyPage = companyService.page(new Page<>(page, limit), wrapper);
        result.setTotal(sysCompanyPage.getTotal());
        result.setRows(sysCompanyPage.getRecords());
        return result;
    }


    /**
     * 添加门店信息
     *
     * @param company
     * @return
     */
    public SysCompany save(SysCompany company) {
        //校验参数
        if (!StringUtils.isNotEmpty(company.getCompanyName())) {
            throw new RuntimeException("门店名称不能为空");
        }
        SysCompany one = companyService.getOne(new LambdaQueryWrapper<SysCompany>()
                .eq(SysCompany::getCompanyName, company.getCompanyName()), false);
        if (one != null) {
            throw new RuntimeException("门店已存在");
        }
        if (!companyService.save(company)) {
            throw new RuntimeException("门店添加失败");
        }
        return company;
    }


    /**
     * 企业修改
     *
     * @param company
     * @return
     */
    public SysCompany update(SysCompany company) {
        if (company.getId() == null) {
            throw new RuntimeException("门店ID不能为空");
        }
        SysCompany one = companyService.getOne(new LambdaQueryWrapper<SysCompany>()
                .eq(SysCompany::getCompanyName, company.getCompanyName()), false);
        if (one != null && !one.getId().equals(company.getId())) {
            throw new RuntimeException("门店已存在");
        }
        if (!StringUtils.isNotEmpty(company.getCompanyName())) {
            throw new RuntimeException("门店名称不能为空");
        }
        return company;
    }

    /**
     * 删除企业
     *
     * @param company
     * @return
     */
    public SysCompany delete(SysCompany company) {
        if (company.getId() == null) {
            throw new RuntimeException("门店ID不能为空");
        }
        List<SysConsumer> consumers = consumerService.list(new LambdaQueryWrapper<SysConsumer>()
                .eq(SysConsumer::getCompanyId, company.getId()));
        if (!consumers.isEmpty()) {
            consumers.forEach(f -> f.setCompanyId(0));
            if (!consumerService.updateBatchById(consumers)) {
                throw new RuntimeException("更新失败");
            }
        }
        SysCompany companySave = companyService.getById(company.getId());
        if (companySave == null) {
            throw new RuntimeException("门店不存在!");
        }
        if (!companyService.removeById(company.getId())) {
            throw new RuntimeException("删除失败");
        }
        return company;
    }

    /**
     * 下拉框选择
     * @param keyword
     * @return
     */
    public List<SysCompany> selectList(String keyword) {
        LambdaQueryWrapper<SysCompany> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(SysCompany::getCompanyName,keyword);
        wrapper.orderByDesc(SysCompany::getId);
        return companyService.list(wrapper);
    }


}
